import addDataStore from '@/store/modules/addDataStore';
import type { AddData } from '@/store/type';
import type { Update } from './useEmitter';

export default (
    tableRef: Ref<TableRef>, // 表格实例
    key: keyof AddData, // 重置数据key
    isUpdate: Update, // 判断是否更新
    cb?: (key: string, payload: anyObj) => void // 开始进行数据对比的回调
) => {
    const route = useRoute();

    const store = addDataStore();

    onActivated(() => {
        isUpdate(setData);
    });

    onUnmounted(() => {
        store.handleAddData(key);
    });

    /**
     * @: 设置数据
     */
    const setData = async () => {
        const tableData = [{}];
        await tableRef.value.setTableData(tableData, true);

        cb && cb(route.name as string, { tableData: tableRef.value?.tableData });
    };
};
